package com.gkzf.ai.module.system.controller.app.appUser;

import com.gkzf.ai.framework.common.pojo.CommonResult;
import com.gkzf.ai.framework.common.util.object.BeanUtils;
import com.gkzf.ai.framework.security.config.SecurityProperties;
import com.gkzf.ai.framework.security.core.util.SecurityFrameworkUtils;
import com.gkzf.ai.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.gkzf.ai.module.system.controller.admin.auth.vo.WxAuthLoginReqVO;
import com.gkzf.ai.module.system.controller.app.appUser.vo.UsersRespVO;
import com.gkzf.ai.module.system.dal.dataobject.appUser.UsersDO;
import com.gkzf.ai.module.system.enums.logger.LoginLogTypeEnum;
import com.gkzf.ai.module.system.service.appUser.UsersService;
import com.gkzf.ai.module.system.service.auth.AdminWxAuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.gkzf.ai.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/app/users")
@Validated
public class UsersController {

    @Resource
    private UsersService usersService;

    @Resource
    private AdminWxAuthService adminWxAuthService;

    @Resource
    private SecurityProperties securityProperties;



    @GetMapping("/get")
    @Operation(summary = "获得用户")
    @Parameter(name = "mpOpenId", description = "小程序用户唯一标识", required = true, example = "1024")
    public CommonResult<UsersRespVO> getUsers(@RequestParam("mpOpenId") String mpOpenId) {
        UsersDO users = usersService.getUsers(mpOpenId);
        return success(BeanUtils.toBean(users, UsersRespVO.class));
    }

    /**
     * 更新用户手机号
     * @param wxAuthLoginReqVO 对象
     * @return 成功
     */
    @PostMapping("/update")
    public CommonResult<Boolean> socialAuthRedirect(@RequestBody @Valid WxAuthLoginReqVO wxAuthLoginReqVO) {
        usersService.updateUserInfo(wxAuthLoginReqVO);
        return success(true);
    }

    @PostMapping("/refresh-token")
    @Operation(summary = "刷新令牌")
    @Parameter(name = "refreshToken", description = "刷新令牌", required = true)
    public CommonResult<AuthLoginRespVO> refreshToken(@RequestParam("refreshToken") String refreshToken) {
        return success(adminWxAuthService.refreshToken(refreshToken));
    }

    @PostMapping("/logout")
    @Operation(summary = "登出系统")
    public CommonResult<Boolean> logout(HttpServletRequest request) {
        String token = SecurityFrameworkUtils.obtainAuthorization(request,
                securityProperties.getTokenHeader(), securityProperties.getTokenParameter());
        if (StringUtils.isNotBlank(token)) {
            adminWxAuthService.logout(token, LoginLogTypeEnum.LOGOUT_SELF.getType());
        }
        return success(true);
    }
}